<?php

namespace app\mobile\controller\info\index;

use app\BaseController;
use app\model\archives_model\UserArchiveModel;
use app\model\archives_model\UserArchiveTypeModel;
use app\model\ConsultOfflineModel;
use app\model\ConsultOnlineModel;
use app\model\SelfEvaluationResultModel;
use app\model\TaskUserResultModel;
use app\model\UserModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 主页数据
     * @return bool|string
     */
    public function data(): bool|string
    {
        try {
            $ConsultOnlineModel = new ConsultOnlineModel();
            $ConsultOfflineModel = new ConsultOfflineModel();
            $TaskUserResultModel = new TaskUserResultModel();
            $SelfEvaluationResultModel = new SelfEvaluationResultModel();
            $online_num = $ConsultOnlineModel->where('uid','=',$this->userData['id'])->count();
            $offline_num = $ConsultOfflineModel->where('uid','=',$this->userData['id'])->count();
            $task_num = $TaskUserResultModel->where('uid','=',$this->userData['id'])->count();
            $self_num = $SelfEvaluationResultModel->where('uid','=',$this->userData['id'])->count();
            return success([
                'online_num'=>$online_num,
                'offline_num'=>$offline_num,
                'task_num'=>$task_num,
                'self_num'=>$self_num
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 修改密码
     * @return bool|string
     */
    public function updatePassWord(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'password|旧密码'  =>  'require|length:8,18',
                'new_password|新密码'  =>  'require|length:8,18',
                'new_password2|新密码'  =>  'require|length:8,18',
            ],[
                'password.require'        => '旧密码不能为空',
                'password.length'        => '旧密码长度不符合要求，8-18位字符',
                'new_password.require'        => '新密码不能为空',
                'new_password.length'        => '新密码长度不符合要求，8-18位字符',
                'new_password2.require'        => '新密码不能为空',
                'new_password2.length'        => '新密码长度不符合要求，8-18位字符',
            ]);
            if ($param['new_password'] != $param['new_password2']) throw new \Exception('两次密码不一致！');
            if ($this->userData['password'] != encrypt_password($param['password'])) throw new \Exception('您输入的旧密码不正确！');
            $UserModel = new UserModel();
            $UserModel->where('id','=',$this->userData['id'])->update([
                'password'=>encrypt_password($param['new_password'])
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 用户档案列表（更新时间）
     * @return bool|string
     */
    public function userProfileList(): bool|string
    {
        try {
            $UserArchiveTypeModel = new UserArchiveTypeModel();
            $UserArchiveModel = new UserArchiveModel();
            $UserModel = new UserModel();
            $data = $UserArchiveTypeModel->field('id,name')->select()->toArray();
            foreach ($data as &$item) {
                if($item['id'] == 1){
                    $item['name'] = '基础资料';
                    $item['up_time'] = $UserModel->where('id','=',$this->userData['id'])->value('update_time');
                }else{
                    $item['up_time'] = $UserArchiveModel->where([
                        ['user_id','=',$this->userData['id']],
                        ['type_id','=',$item['id']]
                    ])->order('update_time','desc')->value('update_time');
                }
            }
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 是否需要自评
     * @return bool|string
     */
    public function isSelf(): bool|string
    {
        try {
            $UserArchiveModel = new UserArchiveModel();
            $is = $UserArchiveModel->where([
                ['user_id','=',$this->userData['id']],
                ['type_id','=',1],
                ['time','>',time() - (30*24*3600)]
            ])->value('id');
            //0不做，1做
            return success($is?0:1);
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


}